package map.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import map.tools.plateform.DevicesSerializer;
import map.tools.plateform.MappingTableSerializer;

import org.eclipse.emf.common.util.EList;

import uimigration.Interactions.Action;
import uimigration.Interactions.Behaviour;
import uimigration.Plateform.Device;
import uimigration.mappingmodel.Event;
import uimigration.mappingmodel.MappingActionEvent;
import uimigration.mappingmodel.MappingBehaviourEvent;
import uimigration.mappingmodel.MappingTable;
import uimigration.mappingmodel.WidgetEvent;

public class MappingTableManager {

	private MappingTable table;
	private List<Device> targetDevice;
	private List<Device> sourceDevice;
	
	public MappingTableManager(String mappingFile, List<String> target, List<String> source){
		table = new MappingTableSerializer().load(new File(mappingFile));
		targetDevice = new ArrayList<Device>();
		sourceDevice = new ArrayList<Device>();
		for(String s : target)
		{
			targetDevice.add(new DevicesSerializer().load(new File(s)));
		}
		for(String s : source)
		{
			sourceDevice.add(new DevicesSerializer().load(new File(s)));
		}

	}
	
	public List<String> findContactEvent(String event)
	{
		return findEquivalentEvent(event, targetDevice.get(0));
	}

	public List<String> findEquivalentEvent(String event,Device td)
	{
		List<String> evt=new ArrayList<String>();;
		EList<Action> la= getMappedAction(event);
		for(MappingActionEvent ae: table.getActionsMapping()){
			for(uimigration.mappingmodel.Event e : ae.getMappedEvents())
				if(e.getIsDeviceEvent()!=null)
				{	for(Device d : e.getIsDeviceEvent())
						if(td.getName().compareTo(d.getName())==0)
							if(equalMappAction(la, e.getMappedActions()))		
								evt.add(e.getName());
				}
			
					
		}
		if (evt.size()==0)
		{
			WidgetEvent we= getEvent(event);
			if(we!=null)
				for(MappingActionEvent ae: table.getActionsMapping()){
					for(Event ev : ae.getMappedEvents())
					{
						if(ev.getIsDeviceEvent()!=null && ev.getIsDeviceEvent().size()>0&&ev.getIsDeviceEvent().get(0).getName().compareTo(td.getName())==0)
						{
							
							if(equivalentActionBehabior(ev, we))
								evt.add(ev.getName());
						}
					}
				}
		}
		return evt;
	}
	
	private WidgetEvent getEvent(String name)
	{
		for(MappingBehaviourEvent bw : table.getBehaviorsMapping())
			for(WidgetEvent w : bw.getMappedEvent() )
				if(w.getName().compareTo(name)==0)
					return w;
		return null;
	}
	private boolean equivalentActionBehabior(Event ed, WidgetEvent eb )
	{
		int mapb=eb.getMappedBehavoiurs().size();
			for(Behaviour b : eb.getMappedBehavoiurs())
				for(Action a: ed.getMappedActions())
					if(equivalentActBeh(a, b))
						mapb--;
		if(mapb<=0)
			return true;
		return false;
	}
	private boolean equivalentActBeh(Action a, Behaviour b)
	{
		Vector<String> vba= new Vector<String>();
		vba.add("ActivateTaskImpl");
		vba.add("ActivateWidgetImpl");
		vba.add("ActivateNavigationImpl");
		vba.add("SelectContentImpl");
		vba.add("SelectWidgetImpl");
		vba.add("SelectContentImpl");
		Vector<String> vbe= new Vector<String>();
		vba.add("EditContentImpl");
		Vector<String> vbm= new Vector<String>();
		vbm.add("MoveWidgetImpl");
		vbm.add("ResizeImpl");
		String action= a.getClass().getSimpleName();
		if(action.contains("ActivateImpl") && vba.contains( b.getClass().getSimpleName()))
			return true;
		else if(action.contains("MoveDeviceImpl") && vbm.contains( b.getClass().getSimpleName()))
			return true;
		else if(action.contains("EnterDataImpl") && vbe.contains( b.getClass().getSimpleName()))
			return true;
			
		return false;

	}
	private EList<Action> getMappedAction(String evt)
	{
		EList<Action> la= null;
		for(MappingActionEvent ae: table.getActionsMapping())
		{
			for(uimigration.mappingmodel.Event e : ae.getMappedEvents())
			{
				if(e.getName().compareTo(evt)==0)
				{
					la=e.getMappedActions();
				}
			}
		}
		return la;
	}
	private boolean equalMappAction(EList<Action> l1,EList<Action> l2)
	{
		if(l1==null ||l2==null)
			return false;
		if(l1.size()==l2.size())
			for(Action a1 : l1)
				for(Action a2 :l2)
				{	
					if(a1.eClass().getName().compareTo(a2.eClass().getName())==0 && a1.getType()==a2.getType())
						return true;
				}
		return false;
	}
}
